package com.fysports.web.controller.app;

import cn.jpush.api.push.PushResult;
import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Page;
import com.fysports.web.controller.AbstractBaseController;
import com.fysports.web.controller.engine.RestFulController;
import com.fysports.web.kit.commons.PathBizKit;
import com.fysports.web.kit.dtgrid.DTGridKit;
import com.fysports.web.kit.dtgrid.DTGridView;
import com.fysports.web.kit.jpush.JpushKit;
import com.fysports.web.model.app.AppPush;
import com.fysports.web.module.core.DefaultModuleBuilder;
import com.fysports.web.module.core.IModuleFactory;
import com.fysports.web.module.view.JsonView;
import com.fysports.web.module.view.OperateType;

import java.util.Map;


/**
 * 目前推送,使用极光推送
 * http://www.jpush.cn/
 * Created by konbluesky
 * Date : 15/7/5 下午10:23
 * Project :jdmdata_server
 */
public class AppPushController extends AbstractBaseController implements RestFulController {
    private IModuleFactory imf = new DefaultModuleBuilder();


    public void index() {
        imf.create(this);
        execute();
        render(PathBizKit.getPageAdminPath("system/push/push_list.ftl"));
    }
    public void load() {
        Map<String,Object> paramMap = Maps.newHashMap();
        paramMap.put(AppPush.COL_LIVEFLAG,"1");
        DTGridView dtgrid = DTGridKit.toDtView(getPara(DTGridKit.PARAM_NAME));
        Page<AppPush> list = AppPush.dao.searchPaginate(dtgrid.getNowPage(), dtgrid.getPageSize(), paramMap);
        renderJson(DTGridKit.toDtView(dtgrid, list));
    }
    @Override
    public void add() {
        setAttr("m", new AppPush());
        render(PathBizKit.getPageAdminPath("system/push/push_add.ftl"));
    }


    /**
     * 保存
     */
    @Override
    public void save() {
        AppPush ap = getModel(AppPush.class, "m");
        boolean status = ap.save();
        JsonView json = new JsonView().setStatus(status);
        json.setMsg(status ? OperateType.SAVE_SUCCESS : OperateType.SAVE_FAIL);
        renderJson(json);
    }

    @Override
    public void edit() {

    }

    @Override
    public void update() {

    }


    /**
     * 考虑到可以多次发送
     */

    public void push() {
        String id = getPara();
        AppPush ap = AppPush.dao.findById(id);

        PushResult pushresult = JpushKit.push(ap);
        if (pushresult == null) {
            renderJson(new JsonView(false, OperateType.OPERATE_FAIL));
            return;
        }

        ap.set(AppPush.COL_RESULT, pushresult.isResultOK() ? 1 : 0);
        ap.update();

        JsonView json = new JsonView().setStatus(pushresult.isResultOK());
        json.setMsg(pushresult.isResultOK() ? OperateType.OPERATE_SUCCESS : OperateType.OPERATE_FAIL);
        renderJson(json);
    }


    /**
     * 删除
     */
    @Override
    public void delete() {
        String uuid = getPara();
        JsonView json = new JsonView();
        AppPush ab = AppPush.dao.findById(uuid);
        boolean status = false;
        if (ab != null) {
            ab.set(AppPush.COL_LIVEFLAG, "0");
            status = ab.update();
        }
        json.setStatus(status);
        json.setMsg(status ? OperateType.OPERATE_SUCCESS : OperateType.OPERATE_FAIL);
        renderJson(json);
    }

}
